import React from 'react';
import Alert from '@material-ui/lab/Alert';
import { KDlg, KInput, ck_int, Dlg, loc, useKForm } from 'sui';
import { get_mod } from './cmd';

const DlgVal = ({ open, setOpen, hcmd, cur, main }) => {
    const form = useKForm();

    const hok = () => {
        const mt = get_mod(cur);

        if (mt === "stell") {
            const ck = {
                num: (v) => ck_int(v, loc("l_658")),
            };

            if (form.isErr(ck)) {
                return;
            }

            setOpen(false);
            hcmd("box", parseInt(form.get("num"), 10));
            return;
        }

        setOpen(false);
        hcmd(null, null)
    };

    const getView = () => {
        const mt = get_mod(cur);

        if (mt === "stell") {
            return (
                <div>
                    <Alert severity="info" className="text-3xl mb-4">{loc("l_451")}</Alert>
                    <KInput label={loc("l_452")} name="num" form={form} className="mx-2"/>
                </div>
            );
        }

        return <Alert severity="warning" className="text-3xl mb-4">{loc("l_730")}</Alert>;
    };

    return (
        <Dlg open={open} hcancel={() => setOpen(false)} hcmd={hok} title={loc("l_724")}>
            {getView()}
        </Dlg>
    );
};

const DlgNum = (props) => {
    const form = useKForm(props.data);

    const ck = {
        num : v => ck_int(v),
    };

    return (
        <KDlg {...props} title={loc("l_127")} ck={ck} form={form}>
            <KInput label={loc("l_127")} form={form} name="num"/>
        </KDlg>
    );
};

export {DlgNum, DlgVal};
